package com.cardbaobao.cardbabyclient.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.annotation.ColorInt;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cardbaobao.cardbabyclient.R;

/**
 * Created by Jiangzx on 2017/9/4 9:34.
 * 用于列表中出现大量item项时，减少layout文件中过多嵌套
 */

public class IntentionLayout extends RelativeLayout {
    private Drawable textDrawableLeft;
    private int textLeftDrawablePadding;
    private int textColorLeft = 0xff222222;
    private CharSequence textLeft = "";

    private Drawable textDrawableRight;
    private int textRightDrawablePadding;
    private int textColorRight = 0xffdddddd;
    private CharSequence textRight = "";

    TextView tvLeft;
    TextView tvRight;

    public IntentionLayout(Context context) {
        this(context, null);
    }

    public IntentionLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public IntentionLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(context).inflate(R.layout.wigdet_intention_layout, this, true);
        setClickable(true);
        tvLeft = (TextView) findViewById(R.id.tv_left);
        tvRight = (TextView) findViewById(R.id.tv_right);
        textLeftDrawablePadding = context.getResources().getDimensionPixelOffset(R.dimen.padding_20);
        textRightDrawablePadding = context.getResources().getDimensionPixelOffset(R.dimen.padding_20);
        TypedArray t = context.obtainStyledAttributes(attrs, R.styleable.IntentionLayout);
        //左边图片
        textDrawableLeft = t.getDrawable(R.styleable.IntentionLayout_textDrawableLeft);
        if (textDrawableLeft == null) {
            textDrawableLeft = ContextCompat.getDrawable(context, R.mipmap.icon_personal_message);
        }

        tvLeft.setCompoundDrawablesWithIntrinsicBounds(textDrawableLeft, null, null, null);
        tvLeft.setCompoundDrawablePadding(
                t.getDimensionPixelOffset(R.styleable.IntentionLayout_textLeftDrawablePadding,
                        textLeftDrawablePadding));

        //左边文字
        textLeft = t.getString(R.styleable.IntentionLayout_textLeft);
        if (TextUtils.isEmpty(textLeft)) {
            textLeft = "";
        } else {
            tvLeft.setText(textLeft);
        }
        //左边文字颜色
        textColorLeft = t.getColor(R.styleable.IntentionLayout_textColorLeft, textColorLeft);
        tvLeft.setTextColor(textColorLeft);

        //右边图片
        textDrawableRight = t.getDrawable(R.styleable.IntentionLayout_textDrawableRight);
        if (textDrawableRight == null) {
            textDrawableRight = ContextCompat.getDrawable(context, R.mipmap.icon_right_gray1);
        }

        tvRight.setCompoundDrawablesWithIntrinsicBounds(null, null, textDrawableRight, null);
        tvRight.setCompoundDrawablePadding(
                t.getDimensionPixelOffset(R.styleable.IntentionLayout_textRightDrawablePadding,
                        textRightDrawablePadding));
        //右边文字
        textRight = t.getString(R.styleable.IntentionLayout_textRight);
        if (TextUtils.isEmpty(textRight)) {
            textRight = "";
        } else {
            tvRight.setText(textRight);
        }
        textColorRight = t.getColor(R.styleable.IntentionLayout_textColorRight, textColorRight);
        tvRight.setTextColor(textColorRight);
        t.recycle();
    }

    public void setTextRight(CharSequence text){
        if(!TextUtils.isEmpty(text)){
            textRight=text;
            tvRight.setText(textRight);
        }
    }

    public void setTextRightColor(@ColorInt int color){
        textColorRight=color;
        tvRight.setTextColor(textColorRight);
    }
}
